﻿$(document).ready(function() {
    if (!Object.keys) Object.keys = function(o) {
        if (o !== Object(o))
            throw new TypeError('Object.keys called on non-object');
        var ret = [], p;
        for (p in o) if (Object.prototype.hasOwnProperty.call(o, p)) ret.push(p);
        return ret;
    }
    var receivedMessageIDs = new Array();
    $.ajax({
        type: "POST",
        url: "/Respond/GetMessages",
        data: { pollID: $('#pollID').val() },
        dataType: "json",
        success: function(response) {
            var keys = Object.keys(response).sort();
            for (var i = 0; i < keys.length; i++) {
                if (!(keys[i] in receivedMessageIDs)) {
                    receivedMessageIDs[keys[i]] = true;
                    $('#respondMessageContainer').prepend("<div class=\"message\">" + response[keys[i]] + "</div>");
                }
            }
        }
    });
    window.setInterval(function() {
        $.ajax({
            type: "POST",
            url: "/Respond/GetMessages",
            data: { pollID: $('#pollID').val() },
            dataType: "json",
            success: function(response) {
                var keys = Object.keys(response).sort();
                for (var i = 0; i < keys.length; i++) {
                    if (!(keys[i] in receivedMessageIDs)) {
                        receivedMessageIDs[keys[i]] = true;
                        $('#respondMessageContainer').prepend("<div class=\"message\">" + response[keys[i]] + "</div>");
                    }
                }
            }
        });
    }, 5000);
});